package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	
	import b1.conifg.HotPoint;
	import b1.scenes.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;

	public class FightObject extends Sprite
	{
		//对应的物理个体
		public var objectB2Body:b2Body;
		//物理碰撞点
		protected var hitPoints:Array;
		private var leftFlag:Boolean;
		
		public function FightObject(leftFlag:Boolean)
		{
			super();
			this.leftFlag = leftFlag;
		}
		
		public function setPosition(px:Number, py:Number):void
		{
			if (objectB2Body != null) 
				objectB2Body.SetPosition(new b2Vec2(px/Fight.worldScale, py/Fight.worldScale));
		}
		
		public function createB2Body(world:b2World, px:Number, py:Number, w:Number, h:Number, density:Number, restitution:Number = 0.5, dynamicFlag:Boolean = true):b2Body
		{
			var objectBodyDef:b2BodyDef = new b2BodyDef();
			objectBodyDef.bullet = false;
			objectBodyDef.position.Set(px/Fight.worldScale, py/Fight.worldScale);
			if (dynamicFlag)
				objectBodyDef.type = b2Body.b2_dynamicBody;
			objectBodyDef.userData = this;
			var objectFixture:b2FixtureDef = new b2FixtureDef();
			objectFixture.restitution = restitution;
			objectFixture.density = density;
			objectB2Body = world.CreateBody(objectBodyDef);
			for(var i:int = 0; i < hitPoints.length; i++)
			{
				//热点半径、长和宽（相对于图片左上角的位置）
				var hotPoint:HotPoint = hitPoints[i];
				//圆形热点
				if (hotPoint.type == 0)
				{
					var x:Number = w * hotPoint.x / 100;
					var y:Number = h * hotPoint.y / 100;
					var r:Number = w * hotPoint.r / 100;
					if (!leftFlag)
						x = -x;
					var circleShape:b2CircleShape = new b2CircleShape(r / Fight.worldScale);
					circleShape.SetLocalPosition(new b2Vec2(x / Fight.worldScale, y / Fight.worldScale));
					objectFixture.shape = circleShape;
				}
				//多边形热点
				else
				{
					var verts:Array = new Array();
					for (var j:int = 0; j < hotPoint.vertices.length; j++)
					{
						var vertex:b2Vec2 = hotPoint.vertices[j];
						x = w * vertex.x / 100;
						y = h * vertex.y / 100;
						if (!leftFlag)
							x = -x;
						verts.push(new b2Vec2(x / Fight.worldScale, y / Fight.worldScale));
					}
					var polyShape:b2PolygonShape = new b2PolygonShape();
					polyShape.SetAsArray(verts, verts.length);
					objectFixture.shape = polyShape;
				}
				objectFixture.userData = hotPoint;
				objectB2Body.CreateFixture(objectFixture);
			}
			this.x = px;
			this.y = py;
			return objectB2Body;
		}
		
		public function destroyB2Body():void
		{
			if (objectB2Body != null)
			{
				objectB2Body.GetWorld().DestroyBody(objectB2Body);
				objectB2Body = null;
			}
		}
		
		public function applyForce(xForce:Number, yForce:Number, torque:Number = 0):void
		{
			objectB2Body.ApplyForce(new b2Vec2(xForce, yForce), objectB2Body.GetWorldCenter());
			objectB2Body.ApplyTorque(torque);
		}
		
		public virtual function onFightTick():void
		{
		}
		
		public virtual function destroy():void
		{
			destroyB2Body();
		}
	}
}
